package com.black.play.component.base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

/**
 * @author ylx
 */
public class AppButton extends JButton
{
    private String text;

    // 背景颜色
    private Color backgroundColor = Color.cyan;

    // 文本颜色
    private Color foregroundColor = Color.BLACK;

    // 圆角半径
    int cornerRadius = 15;

    // 边框宽度和类型
    private Stroke borderStroke = new BasicStroke(2);

    public AppButton(String text)
    {
        this(text, null);
    }

    public AppButton(String text, ActionListener actionListener)
    {
        // super(text); 会绘制一层文本,与手动绘制的同时显示会导致重影
        this.text = text;
        setOpaque(false); // 设置为透明，以便我们自己绘制背景
        setContentAreaFilled(false); // 不填充内容区域，我们将在paintComponent中自定义
        setBorderPainted(false); // 不绘制默认边框
        setFocusPainted(false); // 不在获得焦点时绘制边框
        setPreferredSize(new Dimension(200, 80));
        // 鼠标移入移出事件监听  更改按钮颜色
        addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseEntered(MouseEvent e)
            {
                setBackgroundColor(Color.PINK);
            }

            @Override
            public void mouseExited(MouseEvent e)
            {
                setBackgroundColor(Color.cyan);
            }
        });
        if (actionListener != null)
        {
            addActionListener(actionListener);
        }
    }

    // 按钮绘制
    @Override
    protected void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g.create();

        // 抗锯齿设置
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制背景（圆角矩形）
        g2.setColor(backgroundColor);

        // 圆角半径
        g2.fill(new RoundRectangle2D.Float(0, 0, getWidth() - 1, getHeight() - 1, cornerRadius, cornerRadius));

        // 设置文本颜色和绘制文本
        g2.setColor(foregroundColor);
        Font font = new Font("宋体", Font.PLAIN, 20);
        g2.setFont(font);
        FontMetrics fm = g2.getFontMetrics();
        int x = (getWidth() - fm.stringWidth(text)) / 2;
        int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent();
        g2.drawString(text, x, y);

        // 绘制边框
        g2.setStroke(borderStroke);
        g2.setColor(Color.GRAY); // 边框颜色
        g2.draw(new RoundRectangle2D.Float(0, 0, getWidth() - 1, getHeight() - 1, cornerRadius, cornerRadius));

        g2.dispose();
        super.paintComponent(g);
    }

    public void setText(String text)
    {
        this.text = text;
        repaint();
    }

    // 可以添加更多设置颜色、边框宽度等的方法
    public void setBackgroundColor(Color color)
    {
        this.backgroundColor = color;
        repaint();
    }

    public void setForegroundColor(Color color)
    {
        this.foregroundColor = color;
        repaint();
    }

    public void setCornerRadius(int radius)
    {
        this.cornerRadius = radius;
        repaint();
    }

    public void setBorderWidth(int width)
    {
        this.borderStroke = new BasicStroke(width);
        repaint();
    }
}
